//
// Created by jsjxsy on 2022/8/29.
//

#ifndef CPLUSPLUS_DEMO_TEMPLATE_STACK_H_
#define CPLUSPLUS_DEMO_TEMPLATE_STACK_H_
template<class Type>
class Stack {
 private:
  enum { MAX = 10 };
  Type items[MAX];
  int top;
 public:
  Stack();
  bool isEmpty();
  bool isFull();
  bool push(const Type &item);
  bool pop(Type &item);
};

//模版类声明和实现必须定义在同一个文件中，否则找不到对应的实现
template<class Type>
Stack<Type>::Stack() {
  top = 0;
}

template<class Type>
bool Stack<Type>::isEmpty() {
  return top == 0;
}

template<class Type>
bool Stack<Type>::isFull() {
  return top == MAX;
}

template<class Type>
bool Stack<Type>::push(const Type &item) {
  if (top < MAX) {
    items[top++] = item;
    return true;
  } else {
    return false;
  }
}
template<class Type>
bool Stack<Type>::pop(Type &item) {
  if (top > 0) {
    item = items[--top];
    return true;
  } else {
    return false;
  }
}
#endif //CPLUSPLUS_DEMO_TEMPLATE_STACK_H_
